package com.warmheart.base.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.warmheart.base.config.SysConfig.Log;
import com.warmheart.base.filter.CustomInitFilter;
import com.warmheart.base.filter.ParamLogFilter;
import com.warmheart.base.filter.PreHandlerFilter;

/**
 * 
 * @ClassName: CustomFilterConfiguration
 * @Description: 自定义过滤器配置
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Configuration
@AutoConfigureAfter({ SysConfig.class })
public class CustomFilterConfiguration {

    @Autowired
    private SysConfig sysConfig;

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addExposedHeader("www-authenticate");
        return corsConfiguration;
    }

    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilterRegistrationBean() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        FilterRegistrationBean<CorsFilter> filter = new FilterRegistrationBean<CorsFilter>();
        filter.setName("corsFilter");
        filter.setFilter(new CorsFilter(source));
        // 最高优先级
        filter.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return filter;
    }

    /**
     * 
     * @Title: customInitFilterRegistrationBean
     * @Description: 自定义初始化过滤器设置
     * @author ZhangJianyang
     * @return
     */
    @Bean
    public FilterRegistrationBean<CustomInitFilter> customInitFilterRegistrationBean() {
        FilterRegistrationBean<CustomInitFilter> filter = new FilterRegistrationBean<CustomInitFilter>();
        filter.setName("customInitFilter");
        filter.setFilter(customInitFilter());
        filter.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        return filter;
    }

    /**
     * 
     * @Title: customInitFilter
     * @Description: 自定义初始化过滤器
     * @author ZhangJianyang
     * @return
     */
    @Bean
    public CustomInitFilter customInitFilter() {
        return new CustomInitFilter();
    }

    /**
     * 
     * @Title: paramLogFilterRegistrationBean
     * @Description: 参数、返回值日志过滤器设置
     * @author ZhangJianyang
     * @return
     */
    @Bean
    public FilterRegistrationBean<ParamLogFilter> paramLogFilterRegistrationBean() {
        FilterRegistrationBean<ParamLogFilter> filter = new FilterRegistrationBean<ParamLogFilter>();
        filter.setName("paramLogFilter");
        filter.setFilter(paramLogFilter());
        filter.setOrder(Ordered.HIGHEST_PRECEDENCE + 2);
        return filter;
    }

    /**
     * 
     * @Title: paramLogFilter
     * @Description: 参数、返回值日志过滤器
     * @author ZhangJianyang
     * @return
     */
    @Bean
    public ParamLogFilter paramLogFilter() {
        return new ParamLogFilter() {

            @Override
            protected Log log() {
                return sysConfig.getLog();
            }
        };
    }

    /**
     * 
     * @Title: preHandlerFilterRegistrationBean
     * @Description: 预处理过滤器设置
     * @author ZhangJianyang
     * @return
     */
    @Bean
    public FilterRegistrationBean<PreHandlerFilter> preHandlerFilterRegistrationBean() {
        FilterRegistrationBean<PreHandlerFilter> filter = new FilterRegistrationBean<PreHandlerFilter>();
        filter.setName("preHandlerFilter");
        filter.setFilter(preHandlerFilter());
        filter.setOrder(Ordered.LOWEST_PRECEDENCE);
        return filter;
    }

    /**
     * 
     * @Title: preHandlerFilter
     * @Description: 预处理过滤器
     * @author ZhangJianyang
     * @return
     */
    @Bean
    public PreHandlerFilter preHandlerFilter() {
        return new PreHandlerFilter();
    }
}
